package com.example.opendemo.compent;

import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.videoio.VideoCapture;

import java.io.File;

public class Problem {
    // 加载库
    static   {
        // 加载动态库
        String dir = System.getProperty("user.dir").replace("/", "\\");
        String filePath = "\\dll\\" + "x64" + "\\opencv_java480.dll";
        String path = dir.concat(filePath);
        File file = new File(path);
        if (!file.exists()) {
            throw new RuntimeException("找不到动态库".concat(path));
        }
        System.load(path);
        // System.out.println(path.concat("  load success"));
    }

    public static void main(String[] args) {
        Video();
    }


    // 人脸检测
    public static void Image(){
        // 目标文件
        // "C:\\Users\\kk\\IdeaProjects\\open-demo 需要改成当前本地项目地址
        Mat src= Imgcodecs.imread("C:\\Users\\kk\\IdeaProjects\\open-demo\\src\\main\\resources\\public\\imag\\img.png");
        // 检测结果存放
        Mat dst=SubDetect(src);
        HighGui.imshow("Eyes",dst);
        HighGui.waitKey(0);


    }
    // 眼镜检测--图像
    public static Mat SubDetect(Mat src){
        // 检测模型
        // "C:\\Users\\kk\\IdeaProjects\\open-demo 需要改成当前本地项目地址
        CascadeClassifier Detector=new CascadeClassifier("C:\\Users\\kk\\IdeaProjects\\open-demo\\haarcascades\\haarcascade_frontalface_alt.xml");
        // 检测结果，矩形集
        MatOfRect DetectResult=new MatOfRect();
        Detector.detectMultiScale(src,DetectResult);
        if(DetectResult.toArray().length<=0){
            // 没有检测到匹配的
            return src;
        }
        // 有匹配
        for (Rect rect:DetectResult.toArray()){
            Imgproc.rectangle(src,new Point(rect.x,rect.y),new Point(rect.x+ rect.width, rect.y+rect.height),new Scalar(255,0,0),2);

        }
        return src;
    }

    public static void Video(){
        // 加载匹配模型
        // "C:\\Users\\kk\\IdeaProjects\\open-demo 需要改成当前本地项目地址
        CascadeClassifier faceCascade = new CascadeClassifier("C:\\Users\\kk\\IdeaProjects\\open-demo\\haarcascades\\haarcascade_frontalface_default.xml");
        // 打开摄像头
        VideoCapture videoCapture = new VideoCapture(0);

        while (true) {
            Mat frame = new Mat();
            // 读取帧图像
            videoCapture.read(frame);

            // 将图像转为灰度图
            Mat gray = new Mat();
            Imgproc.cvtColor(frame, gray, Imgproc.COLOR_BGR2GRAY);

            // 检测人脸
            MatOfRect faces = new MatOfRect();
            faceCascade.detectMultiScale(gray, faces);

            // 在检测到的人脸周围画矩形框
            for (Rect rect : faces.toArray()) {
                Imgproc.rectangle(frame, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0), 2);
            }

            // 显示图像
            HighGui.imshow("Frame", frame);

            // 按下q键退出循环
            if (HighGui.waitKey(1) == 'q') {
                break;
            }
        }

        // 释放摄像头
        videoCapture.release();
        // 关闭窗口
        HighGui.destroyAllWindows();
    }
}
